<template> 
	<view>
		<!-- 组件使用 -->
		<!-- orderSummary指的是components:{"orderSummary"} -->
		<ordersummary :orderSummaryArr="orderSummaryArr"></ordersummary>
	</view>
</template>

<script>
	// 引用订单简介模板
	import orderSummary from '../../components/order/orderSummary.vue'
	// 引用公共函数
	import util from '../../utils/util.js'
    import io from '../../utils/uniapp.socket.io.js';
	import route from '../../utils/draw-map.js';
	
	export default {
		//组件注册
		components:{
			"ordersummary":orderSummary
		},
		data() {
			return {
				uid:"1234",
				orderSummaryArr:[
				]
			}
		},
		onLoad() {
			//获取订单
			this.requestOrder();
			//建立socket连接
//			this.socketcon();
		},
		onShow(){
			
		},
		methods: {
			calThirdDistance:function(index,first,secend,third){
				var resone,restwo;
				var distance;
				var that = this;
				console.log(first);
				console.log(secend);
				console.log(third);
				//设置api url
				route.setUrl("https://restapi.amap.com/v3/distance");
				//获取我-取货点的距离 
				resone = route.requestDistance(first,secend)
				resone.then(function (res){
					// console.log(res);
					// console.log(res[0].distance);
					//1.将结果转化为数字，除以1000
					distance = parseInt(res[0].distance) / 1000;
					//2.保留两位，四舍五入，此时distance变量为字符串
					distance = distance.toFixed(2);
                    // that.orderSummaryArr[index].first_route = distance;等同以下两句，只不过用该句数组值确实是对的，但页面不渲染	
					that.$forceUpdate();
                    that.$set(that.orderSummaryArr[index],"first_route",distance);
//					console.log(that.orderSummaryArr[index].first_route);
//					console.log(that.orderSummaryArr);
				})
				
				//获取取货点-送货点的距离
				restwo = route.requestDistance(secend,third)
				restwo.then(function (res){
					// console.log(res);
					// console.log(res[0].distance);
					//1.将结果转化为数字，除以1000
					distance = parseInt(res[0].distance) / 1000;
					//2.保留两位，四舍五入，此时distance变量为字符串
					distance = distance.toFixed(2);
					// that.orderSummaryArr[index].sec_route = distance;等同以下两句，只不过用该句数组值确实是对的，但页面不渲染
                    that.$forceUpdate();
                    that.$set(that.orderSummaryArr[index],"sec_route",distance);
//					console.log(that.orderSummaryArr[index].sec_route);
//					console.log(that.orderSummaryArr);
				})
			},
			requestOrder: function(){
				var that = this;
				var myLocation=[],
					secLocation=[],
					thirLocation=[];
					
				//获取当前定位
				uni.getLocation({
					success(res) {
						var _this = that;
						console.log(res);
						
						myLocation.push((res.longitude).toString());
						myLocation.push((res.latitude).toString());
						
						//发送订单请求
						util.get('horseman/getorder',(res) => {
//							console.log(res);
							_this.orderSummaryArr = res;
							
							for (var i = 0;i < res.length; i ++){
								//将字符串转化为数组
							    secLocation = (res[i].recieve_location).split(',');
							    thirLocation = (res[i].shop.shop_location).split(',');
							    
								_this.orderSummaryArr[i].create_time = util.formatDate(res[i].create_time);
							    // console.log(myLocation);
							    // console.log(secLocation);
							    // console.log(thirLocation);    
								
								_this.calThirdDistance(i,myLocation,secLocation,thirLocation);
							}
							
						});
					},
					fail(err) {
						//提示 '打开定位' 消息
						uni.showModal({
						    title: '提示',
						    content: '请打开手机定位',
						    success: function (res) {
						        if (res.confirm) {
						            console.log('用户点击确定');
						        } else if (res.cancel) {
						            console.log('用户点击取消');
						        }
						    }
						});
					}
				});
				
				
			},
			socketcon: function() {
				var _this = this;
				// 连接服务端
				var socket = io('http://192.168.101.107:2120');
				// socket连接后以uid登录
				socket.on('connect', function() {
					console.log('socket connect')
					console.log(_this.uid);
					socket.emit('login', _this.uid);
				});
				// 后端推送来消息时
				socket.on('new_msg', function(msg) {
					// if (msg.indexOf('解绑') == -1) {
						console.log(msg);
					// };
				});
			},
		}
	}
</script>

<style>

</style>
